<#include "../include/header.html" />
<script type="text/javascript">
    var viewModel = Hap.createGridViewModel("#grid");
    var clientId = "${RequestParameters.clientId}";
    var serverId = "${RequestParameters.serverId}";
    var needSelect = "${RequestParameters.needSelect}";
    var uid = "${RequestParameters.uid}";
    var changeData = null;
    var viewModel2 = kendo.observable({
        confirm: function (e) {
            var profile = window.parent.dataSource.getByUid(uid);
            var changeData = dataSource.data();
            changeData.forEach(function (value) {
                var accessFlag = value.get("accessFlag");
                var accessFrequency = value.get("accessFrequency");
                value.apiAccessLimit["accessFlag"] = accessFlag;
                value.apiAccessLimit["accessFrequency"] = accessFrequency;
                delete value.accessFlag;
                delete value.accessFrequency;
            });

            profile["interfaces"] = changeData;
            window.parent.appSvcGrid.refresh();
            window.parent.$("#editInterface").data("kendoWindow").close();
        },

        close: function (e) {
            window.parent.$("#editInterface").data("kendoWindow").close();
        }
    });
</script>
<body>
<div id="page-content">
    <script>kendo.bind($('#toolbar-btn'), viewModel);</script>
    <div style="clear:both">
        <div id="grid"></div>
    </div>
    <div class="text-right k-window-toolbar" id="gridToolbar">
        <span id="confirm" class="btn btn-primary" style="margin-right: 10px" data-bind="click:confirm"><@spring.message "hap.save"/></span>
        <span id="close" class="btn btn-default" type="button" data-bind="click:close"><@spring.message "hap.cancel"/></span>
        <script>
            kendo.bind($("#confirm"), viewModel2);
            kendo.bind($("#close"), viewModel2);
        </script>
    </div>
</div>

<script type="text/javascript">
    var BaseUrl = _basePath + "/sys/gateway/interface/getInterfacesByServerCode/";
    function save(){
        var grid = $("#grid").data("kendoGrid");
        grid.saveChanges().then(function () {
            window.parent.$('#editInterface').data("kendoWindow").close();
        });
    }
    var dataSource = new kendo.data.DataSource({
        transport: {
            read: {
                url: BaseUrl + clientId + "/" + serverId,
                type: "POST",
                dataType: "json"
            },
            parameterMap: function (options, type) {
                if (type !== "read" && options.models) {
                    var datas = Hap.prepareSubmitParameter(options, type)
                    return kendo.stringify(datas);
                } else if (type === "read") {
                    return Hap.prepareQueryParameter(viewModel.model.toJSON(), options)
                }
            }
        },
        schema: {
            data: 'rows',
            total: 'total',
            model: {
                id: "interfaceId",
                fields: {
                    accessFlag: {
                        from: 'apiAccessLimit.accessFlag',
                        type: 'boolean',
                        checkedValue: 'Y',
                        uncheckedValue: 'N'
                    },
                    accessFrequency: {
                        from: 'apiAccessLimit.accessFrequency',
                        type: "number",
                        validation: {min: 0}
                    }
                },
                editable: function (field) {
                    if (field == "accessFrequency" || field == "accessFlag") {
                        return true;
                    }
                    return false;
                },
            }
        },
        change: function () {
            changeData = this.data;
        }
    });
    var grid = $("#grid").kendoGrid({
        dataSource: dataSource,
        resizable: true,
        scrollable: true,
        navigatable: true,
        height: 420,
        autoBind: false,
        editable: true,
        dataBound: function () {
            if (parent.autoResizeIframe) {
                parent.autoResizeIframe('${RequestParameters.functionCode!}')
            }
        },
        columns: [
            {
                field: "accessFlag",
                attributes: {style: "text-align:center"},
                title: '<@spring.message "api.accesslimit.flag"/>',
                width: 50
            },{
                field: "accessFrequency",
                title: '<@spring.message "api.accesslimit.accessfrequency"/>',
                width: 100
            },{
                field: "enableFlag",
                title: '<@spring.message "hap.enableflag"/>',
                width: 50,
                attributes: {style: "text-align:center"}
            },{
                field: "code",
                title: '<@spring.message "interface.line.interfacecode"/>',
                width: 150
            },{
                field: "name",
                title: '<@spring.message "interface.line.interfacename"/>',
                width: 250
            },{
                field: "clientId",
                hidden: true,
                title: '<@spring.message "api.accesslimit.clientId"/>',
                template: function (rowdata) {
                    if(!rowdata.clientId){
                        rowdata.clientId = clientId;
                    }
                    return rowdata.clientId;
                }
            }
        ],
    }).data("kendoGrid");


    if("true" == needSelect){
        dataSource.read();
    } else {
        // 获取父页面数据
        if(0 != uid) {
            var profile = window.parent.dataSource.getByUid(uid);
            var data = profile["interfaces"];
            var jsonData = JSON.parse(JSON.stringify(data));
            jsonData.forEach(function (value) {
                value['accessFlag'] = value.apiAccessLimit.accessFlag;
                value['accessFrequency'] = value.apiAccessLimit.accessFrequency;
            })
            dataSource.data(jsonData);
        }
    }
</script>
</body>
</html>